package Day2025_03_18;

public class LeetCode647 {
    public int countSubstrings(String s) {
        char[] ss = s.toCharArray();
        int n = ss.length;
        boolean[][] dp = new boolean[n][n];
        int ret = 0;
        for (int i = n - 1; i >= 0; i--) {
            for (int j = i; j < n; j++) {
                if (ss[i] != ss[j]) dp[i][j] = false;
                else {
                    if (i == j || i + 1 == j) dp[i][j] = true;
                    else dp[i][j] = dp[i + 1][j - 1];
                }
                if (dp[i][j]) ret++;
            }
        }
        return ret;
    }
}
